﻿using System;
using System.Data;
using System.Web.UI.WebControls;

using HsTec.Mobile.Green.Util;
using HsTec.Mobile.Green.BLL;
using Baimasoft.Manage.SqlServerDAL;

public partial class ReportAnalysis_TemperatureManage : System.Web.UI.Page
{
    TemperatureBLL bll = new TemperatureBLL();
    Param MyP = new Param();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            btnEdit.Visible = false;
            btnDelete.Visible = false;
            try
            {
                Indicator.Text = Request.QueryString["sIndicator"].ToString();
            }
            catch
            {

            }
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue");
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = " ASC";
            }
            if (ViewState["sortExpression"] == null)
            {
                ViewState["sortExpression"] = String.Empty;
            }
            //this.BindGridViewData();
            this.BindsltArea();
        }
    }
    private void BindGridViewData()
    {
        DataView mydv = bll.GetList(GetSearchString(), rbList.SelectedValue.Trim());
        if (!String.IsNullOrEmpty(ViewState["sortExpression"].ToString()))
            mydv.Sort = ViewState["sortExpression"].ToString() + ViewState["sortDirection"].ToString();
        GridView1.DataSource = mydv;
        GridView1.DataBind();
        HiddenID.Value = "";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!String.IsNullOrEmpty(e.Row.Cells[3].Text.ToString()))
            {
                e.Row.Cells[3].Text = Convert.ToDouble(e.Row.Cells[3].Text).ToString("0.00") + "℃";
            }
        }
    }
    private string GetSearchString()
    {
        string searchStr = "(1=1)";

        string sType = rbList.SelectedValue.Trim();

        string sddlRegionid = sltArea.SelectedValue.ToString().Trim();
        if (sddlRegionid != "")
        {
            searchStr += " and Area_ID  ='" + sddlRegionid + "' ";
        }
        if (sType == "0")
        {
            if (txtYears.Text.Trim() != "")
            {
                searchStr += " and convert(char(10),AddDate,120) ='" + txtYears.Text.Trim() + "' ";
            }
        }
        else
        {
            if (txtYears.Text.Trim() != "")
            {
                searchStr += " and Years ='" + txtYears.Text.Trim().Replace("-", "").Substring(0, 6) + "' ";
            }
        }
        return searchStr;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(HiddenID.Value.Trim()))
        {
            bll.Delete(HiddenID.Value.TrimEnd(','));
            BindGridViewData();
        }
    }
    protected void linkBtnGo_Click(object sender, EventArgs e)
    {

    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "全省");
        sltArea.Items[0].Value = "";
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sortDirection"].ToString() == " ASC")
        {
            ViewState["sortDirection"] = " DESC";
        }
        else
        {
            ViewState["sortDirection"] = " ASC";
        }
        ViewState["sortExpression"] = e.SortExpression;
        this.BindGridViewData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGridViewData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (rbList.SelectedValue == "0")
        {
            btnEdit.Visible = true;
            btnDelete.Visible = true;
        }
        else
        {
            btnEdit.Visible = false;
            btnDelete.Visible = false;
        }
        BindGridViewData();
    }
    protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridViewData();
    }
}
